package io.renren.modules.business.controller;

import java.util.Map;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.renren.modules.business.entity.EDailyPaperEntity;
import io.renren.modules.business.service.EDailyPaperService;
import io.renren.modules.business.service.EDepartmentService;
import io.renren.modules.sys.controller.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 日报表
 */
@RestController
@RequestMapping("/business/edailypaper")
public class EDailyPaperController extends AbstractController {

    @Autowired
    private EDailyPaperService eDailyPaperService;

    @Autowired
    private EDepartmentService eDepartmentService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = eDailyPaperService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{userId}")
    public R info(@PathVariable("userId") Long userId){
		EDailyPaperEntity eDailyPaper = eDailyPaperService.getById(userId);

        return R.ok().put("eDailyPaper", eDailyPaper);
    }

    /**
     * 查询日报列表
     * @param pageNum
     * @param pageSize
     * @param userId
     * @param auditStatus
     * @param dayId
     * @return
     */
    @GetMapping("/query")
    public R queryDailyPaper(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                             @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                             @RequestParam(value = "userId", required = false) Long userId,
                             @RequestParam(value = "auditStatus", required = false) Integer auditStatus,
                             @RequestParam(value = "dayId", required = false) String dayId) {
        if (getUserAuthority() != 1) {
            return R.ok().put("page", eDailyPaperService.queryDailyPaper(pageNum, pageSize, getUserId(), auditStatus, dayId));
        }
        return R.ok().put("page", eDailyPaperService.queryDailyPaper(pageNum, pageSize, userId, auditStatus, dayId));
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody EDailyPaperEntity eDailyPaper){
        Long departmentId = eDailyPaper.getDepartmentId();
        // 查询所在部门
        IPage<Map<String, Object>> department = eDepartmentService.queryDepartment(1, 1, departmentId, null);
        Long leaderId = (Long) department.getRecords().get(0).get("department_leader_id");
        if (leaderId == null) {
            throw new RuntimeException("获取部门信息失败！");
        }
        eDailyPaper.setAuditorId(leaderId);
        eDailyPaperService.save(eDailyPaper);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody EDailyPaperEntity eDailyPaper){
		eDailyPaperService.updateById(eDailyPaper);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestParam(value = "id") Long id){
		eDailyPaperService.removeById(id);

        return R.ok();
    }

}
